package com.doge.service.wx.home;

import com.doge.bean.pojo.ad.Ad;
import com.doge.bean.pojo.ad.AdExample;
import com.doge.bean.pojo.brand.Brand;
import com.doge.bean.pojo.brand.BrandExample;
import com.doge.bean.pojo.category.Category;
import com.doge.bean.pojo.category.CategoryExample;
import com.doge.bean.pojo.coupon.Coupon;
import com.doge.bean.pojo.coupon.CouponExample;
import com.doge.bean.pojo.goods.Goods;
import com.doge.bean.pojo.goods.GoodsExample;
import com.doge.bean.pojo.topic.Topic;
import com.doge.bean.pojo.topic.TopicExample;
import com.doge.bean.vo.wxhome.FloorGoodsListVO;
import com.doge.bean.vo.wxhome.WXIndexVO;
import com.doge.mapper.*;
import com.github.pagehelper.PageHelper;
import lombok.experimental.Helper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: project2-wechat-mall
 * @description:
 * @author: 305905917@qq.com
 * @create: 2021-12-02 11:48
 **/
@Service
public class WxHomeServiceImpl implements WxHomeService {

    @Autowired
    AdMapper adMapper;
    @Autowired
    BrandMapper brandMapper;
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    CouponMapper couponMapper;
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    TopicMapper topicMapper;
    @Autowired
    SystemMapper systemMapper;

    @Override
    public WXIndexVO index() {
        WXIndexVO wxIndexVO = new WXIndexVO();
        //banner
        AdExample adExample = new AdExample();
        adExample.createCriteria().andDeletedEqualTo(false);
        List<Ad> banner = adMapper.selectByExample(adExample);
        wxIndexVO.setBanner(banner);
        //Brand
        String indexBrand = systemMapper.getMarketSystemByKeyName("market_wx_index_brand");
        PageHelper.startPage(1, Integer.valueOf(indexBrand));
        BrandExample brandExample = new BrandExample();
//        brandExample.createCriteria().andIdBetween(1001000, 1001007);
        brandExample.createCriteria().andDeletedEqualTo(false);
        List<Brand> brands = brandMapper.selectByExample(brandExample);
        wxIndexVO.setBrandList(brands);

        //Category
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andPidEqualTo(0).andDeletedEqualTo(false);
        List<Category> categoryList = categoryMapper.selectByExample(categoryExample);
        wxIndexVO.setChannel(categoryList);

        //Coupon
        CouponExample couponExample = new CouponExample();
        categoryExample.createCriteria().andDeletedEqualTo(false);
        List<Coupon> couponList = couponMapper.selectByExample(couponExample);
        wxIndexVO.setCouponList(couponList);


        //floorGoodsList
        String listNum = systemMapper.getMarketSystemByKeyName("market_wx_catlog_list");
        String goodsNum = systemMapper.getMarketSystemByKeyName("market_wx_catlog_goods");
        PageHelper.startPage(1, Integer.parseInt(listNum));
        CategoryExample categoryExample1 = new CategoryExample();
        categoryExample1.createCriteria().andLevelEqualTo("L1").andDeletedEqualTo(false);
        List<Category> categoryList1 = categoryMapper.selectByExample(categoryExample1);
        List<FloorGoodsListVO> floorGoodsListVOS = new ArrayList<>();
        for (Category category1 : categoryList1) {
            CategoryExample categoryExample2 = new CategoryExample();
            categoryExample2.createCriteria().andPidEqualTo(category1.getId()).andDeletedEqualTo(false);
            List<Category> categoryList2 = categoryMapper.selectByExample(categoryExample2);
            for (Category category : categoryList2) {
                PageHelper.startPage(1, Integer.parseInt(goodsNum));
                GoodsExample goodsExample = new GoodsExample();
                goodsExample.createCriteria().andCategoryIdEqualTo(category.getId()).andDeletedEqualTo(false);
                List<Goods> goodsList = goodsMapper.selectByExample(goodsExample);
                //List<Goods> collect = goodsList.stream().limit(6).collect(Collectors.toList());
                if (goodsList.size()!=0){
                    floorGoodsListVOS.add(new FloorGoodsListVO(goodsList, category1.getId(), category1.getName()));
                }

                if (goodsList.size() == Integer.parseInt(goodsNum)) {
                    break;
                }

            }

        }
        wxIndexVO.setFloorGoodsList(floorGoodsListVOS);

        String hot = systemMapper.getMarketSystemByKeyName("market_wx_index_hot");
        PageHelper.startPage(1, Integer.valueOf(hot));
        //isHot
        GoodsExample goodsExample = new GoodsExample();
        goodsExample.createCriteria().andIsHotEqualTo(true).andDeletedEqualTo(false);
        List<Goods> goodsList = goodsMapper.selectByExample(goodsExample);
        wxIndexVO.setHotGoodsList(goodsList);

        String aNew = systemMapper.getMarketSystemByKeyName("market_wx_index_new");
        //isNew
        PageHelper.startPage(1, Integer.valueOf(aNew));
        GoodsExample goodsExample1 = new GoodsExample();
        goodsExample1.createCriteria().andIsNewEqualTo(true).andDeletedEqualTo(false);
        List<Goods> goodsList1 = goodsMapper.selectByExample(goodsExample1);
        wxIndexVO.setNewGoodsList(goodsList1);

        String indexTopic = systemMapper.getMarketSystemByKeyName("market_wx_index_topic");
        PageHelper.startPage(1, Integer.parseInt(indexTopic));
        //topic
        TopicExample topicExample = new TopicExample();
        topicExample.createCriteria().andDeletedEqualTo(false);
        List<Topic> topics = topicMapper.selectByExample(topicExample);
        wxIndexVO.setTopicList(topics);

        return wxIndexVO;

    }
}
